package com.shouer.doc.ui.bindwechat.presenter;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.shouer.doc.request.GsonRequest;
import com.shouer.doc.request.RequestUtil;
import com.shouer.doc.ui.bindwechat.response.BindWeChatResponse;
import com.shouer.doc.ui.bindwechat.view.IBindWeChatView;
import com.shouer.doc.ui.forgetpassword.response.CheckSMSResponse;
import com.shouer.doc.ui.forgetpassword.response.GetSmsResponse;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by YYY on 2017/6/5.
 */

public class BindWeChatPresenterImpl implements IBindWeChatPresenter {


    private IBindWeChatView mView;
    public BindWeChatPresenterImpl(IBindWeChatView mView)
    {
        this.mView=mView;
        EventBus.getDefault().register(this);
    }

    @Override
    public void getSMS(String userPhone) {
        Map<String, String> param = new HashMap<>();
        param.put("userPhone", userPhone);

        final GsonRequest<GetSmsResponse> request = new GsonRequest("getSMS", GetSmsResponse.class, new Response.Listener<GetSmsResponse>() {
            @Override
            public void onResponse(GetSmsResponse response) {

                EventBus.getDefault().post(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                mView.getSmsFailed("网络异常 获取校验码失败");
            }
        });
        RequestUtil.sendRequest(mView.getContext(), param, request);
    }

    @Override
    public void checkSMS(String userPhone, String sms) {
        Map<String, String> param = new HashMap<>();
        param.put("userPhone", userPhone);
        param.put("sms", sms);

        final GsonRequest<CheckSMSResponse> request = new GsonRequest("checkSMS", CheckSMSResponse.class, new Response.Listener<CheckSMSResponse>() {
            @Override
            public void onResponse(CheckSMSResponse response) {

                EventBus.getDefault().post(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                mView.checkNumFailed("网络异常 校验失败");
            }
        });
        RequestUtil.sendRequest(mView.getContext(), param, request);
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void bindWeChat(String phone, String accressToken, String refreshToken) {
        Map<String, String> param = new HashMap<>();
        param.put("userPhone", phone);
        param.put("refreshToken", refreshToken);
        param.put("wxToken", accressToken);
        param.put("tag", "1");


        final GsonRequest<BindWeChatResponse> request = new GsonRequest("bindWeChatAccount", BindWeChatResponse.class, new Response.Listener<BindWeChatResponse>() {
            @Override
            public void onResponse(BindWeChatResponse response) {

                EventBus.getDefault().post(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                mView.bindWeChatFailed("网络异常 绑定手机号失败");
            }
        });
        RequestUtil.sendRequest(mView.getContext(), param, request);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(GetSmsResponse response) {
        if (response.getErrorCode() >= 0) {
            mView.getSmsSuccess();
        } else {
            mView.getSmsFailed(response.getErrorMsg());
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(CheckSMSResponse response) {

        if (response.getErrorCode() >= 0) {
            mView.checkNumSuccess(response);
        } else {
            mView.checkNumFailed(response.getErrorMsg());
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(BindWeChatResponse response) {
        if (response.getErrorCode() >= 0) {
            mView.bindWeChatSuccess();
        } else {
            mView.bindWeChatFailed(response.getErrorMsg());
        }
    }
}
